package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import cn.edu.nju.ws.sview.wyge.relations.adataset.entities.GetOthers;
import cn.edu.nju.ws.sview.wyge.relations.adataset.entities.ISentence;
import cn.edu.nju.ws.sview.wyge.relations.adataset.entities.GetNamedEntities;
import cn.edu.nju.ws.sview.wyge.relations.adataset.entities.SnippetDisplay;
import cn.edu.nju.ws.sview.wyge.relations.adataset.entities.qxliu.algorithms.ClassTree;
import cn.edu.nju.ws.sview.wyge.util.SubjectComparator;

public class EntitiesView extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String snippet = "";
		String ds_type = request.getParameter("ds_type");
		String idStr = request.getParameter("idStr");
		int ds_id = Integer.valueOf(request.getParameter("ds_id"));
		int class_id = Integer.valueOf(request.getParameter("class_id"));
		String param = request.getParameter("intent");
		if(param == null){
			String lang = request.getParameter("lang");
			response.setContentType("text/html;charset=utf-8");
			ArrayList<ISentence> sents = new ArrayList<ISentence>();
			if(ds_id>0){
				if (class_id == 0) {//named entity
	//				String showedSub = request.getParameter("showed_sub");
	//				sents = GetNamedEntities.findNoTyped(ds_type, ds_id, showedSub);
					sents = GetNamedEntities.find(ds_type, ds_id);
				} else if (class_id == -1) {//others
					sents = GetOthers.find(ds_type, ds_id);
				} else if (class_id == -2) {//all
					sents = GetNamedEntities.find(ds_type, ds_id);
					sents.addAll(GetOthers.find(ds_type, ds_id));
				}else {
	//				sents = GetNamedEntities.findIdentical(ds_type, ds_id, class_id);
					sents = GetNamedEntities.find(ds_type, ds_id, class_id);
				}
				Collections.sort(sents);//=========sort
				for (ISentence sent : sents) {
					String resource = sent.getSubject();
					ArrayList<ArrayList<String>> triples = sent.getTriples();
//					String res = SnippetDisplay.showSnippet(resource, triples, "en", ds_type, ds_id);
					String res = SnippetDisplay.showDivSnippet(resource, triples, lang, ds_type, ds_id);
					snippet += res;
				}
			}else if(idStr != null){
				String[] strList = idStr.split("_");
				ArrayList<Integer> docs = new ArrayList<Integer>();
				for(String id :strList){
					if(id.trim().length()>0){
						docs.add(Integer.parseInt(id));
					}
				}
				if (class_id == 0) {//named entity
					sents = GetNamedEntities.find(docs);
				} else if (class_id == -1) {//others
					sents = GetOthers.find(docs);
				} else if (class_id == -2) {//all
					sents = GetNamedEntities.find(docs);
					sents.addAll(GetOthers.find(docs));
				}else {
	//				sents = GetNamedEntities.findIdentical(ds_type, ds_id, class_id);
					ArrayList<Integer> classes = new ClassTree(docs).getChildren(class_id);
					classes.add(class_id);
					sents = GetNamedEntities.find(docs, classes);
				}
				Collections.sort(sents);//=========sort
				for (ISentence sent : sents) {
					String resource = sent.getSubject();
					ArrayList<ArrayList<String>> triples = sent.getTriples();
//					String res = SnippetDisplay.showSnippet(resource, triples, "en", ds_type, ds_id);
					String res = SnippetDisplay.showDivSnippet(resource, triples, lang, docs);
					snippet += res;
				}
			}
			response.getWriter().print(snippet);
		}else if(param.contentEquals("getEntityIds")){
			response.setCharacterEncoding("utf-8");
			ArrayList<String> eids = new ArrayList<String>();
			if(ds_id>0){
				if (class_id == 0) {
	//				String showedSub = request.getParameter("showed_sub");
	//				sents = GetNamedEntities.findNoTyped(ds_type, ds_id, showedSub);
					eids = GetNamedEntities.findEntityIds(ds_type, ds_id);//.find(ds_type, ds_id);
				} else if (class_id == -1) {
					eids = GetOthers.findEntityIds(ds_type, ds_id);//.find(ds_type, ds_id);
				} else if (class_id == -2) {
					eids = GetNamedEntities.findEntityIds(ds_type, ds_id);
					eids.addAll(GetOthers.findEntityIds(ds_type, ds_id));
				}else {
	//				sents = GetNamedEntities.findIdentical(ds_type, ds_id, class_id);
					eids = GetNamedEntities.findEntityIds(ds_type, ds_id, class_id);//.find(ds_type, ds_id, class_id);
				}
			}else if(idStr != null){
				String[] strList = idStr.split("_");
				ArrayList<Integer> docs = new ArrayList<Integer>();
				for(String id :strList){
					if(id.trim().length()>0){
						docs.add(Integer.parseInt(id));
					}
				}
				if (class_id == 0) {
	//				String showedSub = request.getParameter("showed_sub");
	//				sents = GetNamedEntities.findNoTyped(ds_type, ds_id, showedSub);
					eids = GetNamedEntities.findEntityIds(docs);//.find(ds_type, ds_id);
				} else if (class_id == -1) {
					eids = GetOthers.findEntityIds(docs);//.find(ds_type, ds_id);
				} else if (class_id == -2) {
					eids = GetNamedEntities.findEntityIds(docs);
					eids.addAll(GetOthers.findEntityIds(docs));
				}else {
	//				sents = GetNamedEntities.findIdentical(ds_type, ds_id, class_id);
					ArrayList<Integer> classes = new ClassTree(docs).getChildren(class_id);
					classes.add(class_id);
					eids = GetNamedEntities.findEntityIds(docs, classes);//.find(ds_type, ds_id, class_id);
				}
			}
			Collections.sort(eids, new SubjectComparator());
			JSONArray array = new JSONArray();
			for(String eid: eids){
				array.add(eid);
			}
			JSONObject result = new JSONObject();
			result.put("array", array);
//			System.out.println("rs:"+result.toJSONString());
			response.getWriter().print(result);
		}
	}
	
	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}
}
